package com.example.basic;

import java.util.concurrent.TimeUnit;

public class SleepTest implements Runnable{
    private Integer ticketNum=100;
    @Override
    public void run() {
        while (true) {
            try {
                this.takingTicket();
                TimeUnit.MILLISECONDS.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (ticketNum < 1) {
                break;
            }
        }
    }
    public synchronized void takingTicket ()  {
            System.out.println(Thread.currentThread().getName()+"抢到第"+ticketNum+"张票");
            ticketNum--;
        }
    public static void main(String[] args) {
        SleepTest sleepTest = new SleepTest();
        Thread t1 = new Thread(sleepTest, "小明");
        Thread t2 = new Thread(sleepTest, "小红");
        t1.start();
        t2.start();
    }
}
